;DEFINICIONES
#define GROUPID_ASTEROID_BELT 9
#define GROUPID_TRACTOR_BEAM 650
#define TYPEID_SALVAGER2 30836
#define TYPEID_SALVAGER 25861
#define GROUPID_DATA_MINER 538
#define GROUPID_AFTERBURNER 46
#define GROUPID_CLOAK 330
#define GROUPID_WRECK 186

variable string State = "STOP"
variable int TractorCount
variable int SalvagerCount
variable int Tics = 35

	variable(global) iterator Target
	variable(global) index:entity Targets
;	variable(script) iterator Target

	variable obj_Modules Modules
;	variable obj_MoveShip MoveShip
	variable obj_Belts Belts
	variable obj_Wrecks Wrecks
	variable obj_Target Targeting
	variable obj_Ship Ship
;	variable obj_Gui Gui
	variable obj_Salvaging Salvaging
	
function main()
{
	;ui -load gui/gui.xml
	echo "	Max Targets: ${Me.MaxLockedTargets}"
	echo " "
	Ship:States
	while TRUE
	{
		wait ${Tics}
		if ${Me.ToEntity.Mode} == 3
		{
			State:Set[WARP]
			Tics:Set[45]
		}
		if ${Me.ToEntity.Mode} == 2 && ${State.Equal["WARP"]}
		{
			Tics:Set[30]
			State:Set[STOP]
		}
		;Ship:States
		call States
	}
	while ${salvager.Pause}
	{
		wait 5
	}
}
;==================================================================;
;=========================== SHIP STATES ==========================;
;==================================================================;
/*objectdef obj_Gui
{
	method Final()
	{
		ui -unload gui/gui.xml
		;Script:End
	}
}*/
function States()
{	
	switch ${State}
	{
		case EMPTY
			echo "	State: ${State}"
			break
		case WARP
			echo "	State: ${State}"
			break
		case PAUSE
			echo "	State: ${State}"
			Script:Pause
			break
			
		case STOP
			echo "	State: ${State}"
			if ${Wrecks.Count} == 0
			{
				echo "	Wrecks not found warp next belt"
				State:Set[NOTHING]
			}
			else 
			{
				echo "	${Wrecks.Count} wrecks found."
				State:Set[WRECKS]
			}
			break
		case NOTHING
			echo "	State: ${State}"
			Belts:NextBelt
			Ship:WarpBelt
			break
		case WRECKS
			echo "	State: ${State}"
			Targeting:DoIt
			break
		case TRACTOR
			echo "	State: ${State}"
			if ${Tics} == 35
			{
				Tics:Set[30]
			}
			call DoTractor
			break
		case SALVAGE
			echo "	State: ${State}"
			break
		default
			Tics:Set[35]
			break
	}
}

objectdef obj_Ship
{
	method WarpBelt()
	{
		switch ${State}
		{
			case NOTHING
				echo "	Warp to: ${Belts.Belt.Value.Name}"
				Belts.Belt.Value:WarpTo[0]
				break
			default
				break
		}
	}
}
;==================================================================;
;============================= MODULES ============================;
;==================================================================;
objectdef obj_Modules
{
	variable	int				ModulesCount
	variable	index:module	ModuleList
	variable	index:module	Tractor
	variable	index:module	Salvager
	variable	index:module	Afterburner
	variable	index:module	Cloaking
	
	method Initialize()
	{
		variable int GroupID
		variable int TypeID
		variable iterator Module

		TractorBeam:Clear
		Cloaking:Clear
		Salvager:Clear
		Afterburner:Clear
		This.ModuleList:Clear
		Me.Ship:DoGetModules[ModuleList]
		
		if !${This.ModuleList.Used} && ${Me.Ship.HighSlots} > 0
		{
			echo "	ERROR - Don have any modules fitting: ${Me.Ship.HighSlots} and ${This.ModuleList.Used} ."
			return
		}

		This.ModuleList:GetIterator[Module]

		if (${Module:First(exists)})
		{
			do
			{
				GroupID:Set[${Module.Value.ToItem.GroupID}]
				TypeID:Set[${Module.Value.ToItem.TypeID}]

			;echo "	ID MOD: ${Module.Value.ToItem.TypeID}"
				switch ${GroupID}
				{
					case GROUPID_TRACTOR_BEAM
						This.Tractor:Insert[${Module.Value}]
						echo "	Tractor detected: ${Module.Value.ToItem.Slot}  ${Module.Value.ToItem.Name}"
						ModulesCount:Inc
						TractorCount:Inc
						break        
					case GROUPID_DATA_MINER
						if ${TypeID} == TYPEID_SALVAGER
							{
								This.Salvager:Insert[${Module.Value}]
								echo "	Salvager detected: ${Module.Value.ToItem.Slot}  ${Module.Value.ToItem.Name}"
								ModulesCount:Inc
								SalvagerCount:Inc
							}
							elseif ${TypeID} == TYPEID_SALVAGER2
							{
								This.Salvager:Insert[${Module.Value}]
								echo "	Salvager detected: ${Module.Value.ToItem.Slot}  ${Module.Value.ToItem.Name}"
								ModulesCount:Inc
								SalvagerCount:Inc
							}
						break
					case GROUPID_AFTERBURNER
						This.Afterburner:Insert[${Module.Value}]
						echo "	Slot: ${Module.Value.ToItem.Slot}  ${Module.Value.ToItem.Name}"
						ModulesCount:Inc
						break
					
					case GROUPID_CLOAK
						This.Cloaking:Insert[${Module.Value}]
						echo "	Slot: ${Module.Value.ToItem.Slot}  ${Module.Value.ToItem.Name}"
						ModulesCount:Inc
						break
					
					default
						break
				}
			}
			while ${Module:Next(exists)}
			echo "  "
			echo "================== FITTING =================="
			echo "	${ModulesCount} - High Slot modules fitting"
			echo "	${TractorCount} - Tractors detected"
			echo "	${SalvagerCount} - Salvagers detected"
			echo "============================================="
			echo "  "
		}	
	}

	method Tractor(int64 TargetDistance, int64 IDTarget, int64 WreckID)
	{
		variable iterator Module
		variable int k
		variable int y
		Tractor:GetIterator[Module]
		Salvager:GetIterator[Module]
		
		for (k:Set[1] ; ${k} <= ${TractorCount} ; k:Inc)
		{
			;echo "	ID: ${IDTarget} Dist: ${TargetDistance} ID: ${WreckID} Tractor: ${Tractor.Get[${k}].TargetID}"
			if ${Tractor.Get[${k}].IsActive} && ${Tractor.Get[${k}].TargetID} == ${WreckID} && ${Modules.OptimalSalvageRange} < ${TargetDistance}
			{
				echo "	Tractor Skip ${k}: Working..."
				echo "	"
				continue
			}
	 		elseif !${Tractor.Get[${k}].IsActive} && ${Modules.OptimalSalvageRange} < ${TargetDistance} && ${Tractor.Get[${k}].TargetID} != ${WreckID}
			{	
			;	echo "== Tractoring ==============================="
				echo "	Tractor ${k}: ${IDTarget}"
			;	echo "============================================="
				echo "	"
				
				Tractor.Get[${k}]:Activate[${IDTarget}]    
				break
			}
			elseif ${Tractor.Get[${k}].IsActive} && ${Modules.OptimalSalvageRange} > ${TargetDistance} && ${Tractor.Get[${k}].TargetID} == ${WreckID}
			{
			;	echo "= Stop Tractor =============================="
				echo "	Tractor Stop ${k}: ${Tractor.Get[${k}].TargetID}"
			;	echo "	Wreck ID: ${IDTarget}"
				echo "	"
				Tractor.Get[${k}]:Deactivate
				break
			}
		}
		for (y:Set[1] ; ${y} <= ${SalvagerCount} ; y:Inc)
		{
			if ${Salvager.Get[${y}].IsActive} && ${Salvager.Get[${y}].TargetID} == ${WreckID}
			{
				echo "	Salvager Skip ${y}: Working..."
				echo "	"
				continue
			}
 			elseif  ${Modules.OptimalSalvageRange} > ${TargetDistance} && !${Salvager.Get[${y}].IsActive} && ${Salvager.Get[${y}].TargetID} != ${WreckID}
			{
			;	echo "== Salvaging ================================"
				echo "	Salvager ${y}: ${IDTarget}"
			;	echo "============================================="
				echo "	"
				Salvager.Get[${y}]:Activate[${IDTarget}]
				break
			}
		}
	}
	member:int OptimalTractorRange()
	{
		if !${Module(exists)}
		{
			return 0
		}
		variable iterator Module
		Tractor:GetIterator[Module]
		
		if ${Module:First(exists)}
		{
			return ${Module.Value.OptimalRange}
			
		}
		return 0
	}
	member:int OptimalSalvageRange()
	{
		if !${Module(exists)}
		{
			return 0
		}
		variable iterator Module
		Salvager:GetIterator[Module]
		
		if ${Module:First(exists)}
		{
			return ${Module.Value.OptimalRange}			
		}
		return 0
	}
}

;==================================================================;
;============================= ACTION =============================;
;==================================================================;
function DoTractor(int64 Targt, int64 Dist)
{
	variable index:entity Targets
	variable iterator Target
	EVE:DoGetEntities[Targets, byType, GroupID, GROUPID_WRECK]
	Targets:GetIterator[Target]
	Targeting:DoIt	
	echo "	"
	echo "============================================="
	echo "	Wrecks Target: ${Wrecks.Target}"
	echo "============================================="
	echo " "
		
	if ${Target:First(exists)}
	{
		do
		{
			if ${Target.Value.IsLockedTarget} && ${Target.Value.Distance} < ${Modules.OptimalTractorRange}
			{
				Target.Value:MakeActiveTarget
				Modules:Tractor[${Target.Value.Distance}, ${Target.Value}, ${Target.Value.ID}]
			}
			elseif ${Target.Value.IsLockedTarget} && ${Target.Value.Distance} > ${Modules.OptimalTractorRange}
			{
				Target.Value:UnlockTarget
				if ${Wrecks.Target} == 0
				{
					echo "	Wreck Not a Range. Skip Belt."
					echo "	"
					State:Set[NOTHING]
				}
			}
			wait 8
		}
		while ${Target:Next(exists)}
		}
	else 
	{
		State:Set[STOP]
	}
}


objectdef obj_Salvaging
{

}

objectdef obj_Belts
{
	variable index:entity Belts
	variable iterator Belt
	
	method Initialize()
	{
		EVE:DoGetEntities[Belts, byType, GroupID, GROUPID_ASTEROID_BELT]
		Belts:GetIterator[Belt]

		variable int CounterB
		CounterB:Set[0]
		echo "=========== BELTS IN SYSTEM ================="
		if ${Belt:First(exists)}
		do
		{
			echo "	${Belt.Value.Name}"
			CounterB:Inc[1]
		}
		while ${Belt:Next(exists)}
		echo "============================================="
		echo "	${CounterB} belts count..."
		echo " "
		
	}	
	method NextBelt()
	{
		if !${Belt:Next(exists)}
			Belt:First(exists)
		return
	}	
}
objectdef obj_Wrecks
{
	member:int Count()
	{
		variable index:entity Wrecks
		variable iterator Wreck
		variable int Count
		
		EVE:DoGetEntities[Wrecks, GroupID, GROUPID_WRECK]
		Wrecks:GetIterator[Wreck]

		Count:Set[0]
		
		if ${Wreck:First(exists)}
		do
		{
			Count:Inc[1]
		}
		while ${Wreck:Next(exists)}
		
		if ${Count} > 0
		{
			return ${Count}
		}
		return 0
	}
	member:int Target()
	{
		variable index:entity Targets
		variable int count = 0
		EVE:DoGetEntities[Targets, GroupID, GROUPID_WRECK]
		Targets:GetIterator[Target]
		
		if ${Target:First(exists)}
		{	
			do
			{	
				if ${Target.Value.IsLockedTarget}
				{
					count:Inc[1]
				}
			}
			while ${Target:Next(exists)}
			return ${count}
		}
	}
}
objectdef obj_Target
{
	
	
	method DoIt()
	{
		variable iterator Target
		variable index:entity Targets
		EVE:DoGetEntities[Targets, byType, GroupID, GROUPID_WRECK]
		Targets:GetIterator[Target]
		variable int T = 0
		echo "	Search valid Targets..."

		if ${Target:First(exists)}
		do
		{	
			if ${Wrecks.Target} < ${Me.MaxLockedTargets}
			{
				if !${Target.Value.IsLockedTarget} && !${Target.Value.BeingTargeted} && !${Target.Value.HaveLootRights} 
				{
					if ${Target.Value.Distance} < ${Modules.OptimalTractorRange} && ${T} < ${Me.MaxLockedTargets}
					{
						if !${Target.Value.Name.Find["Hauler"](exists)} && !${Target.Value.Name.Find["Carrier"](exists)} && !${Target.Value.Name.Find["Convoy"](exists)}
						{
							echo "	Targeting: ${Target.Value}"
							Target.Value:LockTarget
							T:Inc[1]
							State:Set[TRACTOR]
						}
						else
						{
							State:Set[NOTHING]
						}
							
					}
				}
			}
			if ${Target.Value.IsLockedTarget}
			{
				State:Set[TRACTOR]
			}
			if ${Target.Value.HaveLootRights} && ${Wrecks.Target} == 0
			{
				echo "Don't have loot rights"
				State:Set[NOTHING]
			}
		}
		while ${Target:Next(exists)}
	}
}